package com.iflytek.aidemo.teacher;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.iflytek.aidemo.R;
import com.iflytek.aidemo.model.Student;

import java.util.List;

public class StudentManagementAdapter extends RecyclerView.Adapter<StudentManagementAdapter.ViewHolder> {

    private final List<Student> students;
    private final OnStudentManagementListener listener;

    public interface OnStudentManagementListener {
        void onDeleteClick(Student student);
        void onItemClick(Student student);
    }

    public StudentManagementAdapter(List<Student> students, OnStudentManagementListener listener) {
        this.students = students;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_student_management_simple, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Student student = students.get(position);
        // 显示学生ID和姓名
        holder.tvId.setText("ID: " + student.getId());
        holder.tvName.setText("姓名: " + student.getName());

        holder.itemView.setOnClickListener(v -> listener.onItemClick(student));
        holder.btnDelete.setOnClickListener(v -> listener.onDeleteClick(student));
    }

    @Override
    public int getItemCount() {
        return students.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvId, tvName;
        Button btnDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvId = itemView.findViewById(R.id.tv_student_id);
            tvName = itemView.findViewById(R.id.tv_student_name);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
}